package qualomelhor.game;

import static playn.core.PlayN.*;
import playn.core.GroupLayer;
import playn.core.Image;
import playn.core.ImageLayer;
import playn.core.Pointer;
import playn.core.ResourceCallback;

public class Pea {
	public static String IMAGE = "/images/pontodir.png";
	private ImageLayer layer;
	private float angle;
	private float xPos, yPos;

	public Pea(final GroupLayer peaLayer, final float x, final float y) {
		Image image = assets().getImage(IMAGE);
		layer = graphics().createImageLayer(image);
		xPos = x;
		yPos = y;

		layer.addListener(new Pointer.Adapter() {
	    	@Override
	    	public void onPointerEnd(Pointer.Event event) {
	    		xPos += 50;
	    		yPos += 50;
	    	}
		});

		
		// Add a callback for when the image loads.
		// This is necessary because we can't use the width/height (to center the
		// image) until after the image has been loaded
		image.addCallback(new ResourceCallback<Image>() {
			@Override
			public void done(Image image) {
				layer.setOrigin(image.width() / 2f, image.height() / 2f);
				layer.setTranslation(x, y);
				peaLayer.add(layer);
			}

			@Override
			public void error(Throwable err) {
				log().error("Error loading image!", err);
			}
		});
	}

	public void update(float delta) {
		angle += delta;
		layer.setRotation(angle);
		layer.setTranslation(xPos, yPos);
	}
}